<template>
  <div class="refine_dcustom">
    <h2 class="titleblue">自定义</h2>
    <div class="contant">
      <el-tabs v-model="activeName2" @tab-click="handleClick">
        <el-tab-pane label="任务类型" name="1">
          <router-view ref="taskType" name="TaskType" :taskType="taskType" @update="getData(0)"></router-view>
        </el-tab-pane>
        <el-tab-pane label="专项任务" name="2">
          <router-view ref="specialTask" name="SpecialTask" :specialTask="specialTask" :totalnum1="totalnum1"
                       @update="getData(0)"></router-view>
        </el-tab-pane>
        <el-tab-pane label="任务对象类型" name="3">
          <router-view ref="taskObject" name="TaskObject" :taskObject="taskObject" @update="getData(0)"></router-view>
        </el-tab-pane>
        <el-tab-pane label="用户证书" name="4">
          <router-view ref="userCertificate" name="UserCertificate" :totalnum2="totalnum2" :certificate="certificate"
                       @update="getData(0)"></router-view>
        </el-tab-pane>
        <el-tab-pane label="巡航里程" name="5">
          <router-view ref="cruisingMileage" name="CruisingMileage" :mileage="mileage" :organiza="organiza"
                       :totalnum3="totalnum3" @update="getData(0)"></router-view>
        </el-tab-pane>
        <el-tab-pane label="处理措施" name="6">
          <router-view ref="treatmentMeasures" name="TreatmentMeasures" :measures="measures"
                       @update="getData(0)"></router-view>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<script>
  import taskTypeService from '@/services/TaskTypeService.js'
  import plansService from '@/services/PlansService.js'
  import taskObjectService from '@/services/TaskObjectService.js'
  import punitiveMeasuresService from '@/services/PunitiveMeasuresService.js'
  import cruiseService from '@/services/CruiseService.js'
  import * as orgService from '@/services/OrganizationService'
  import dictService from '@/services/DictionaryService.js'

  export default {
    name: 'Dcustom',
    data: function () {
      return {
        activeName2: '1',
        taskType: [],
        specialTask: [],
        taskObject: [],
        measures: [],
        mileage: [],
        organiza: [],
        certificate: [],
        totalnum1: 1,
        totalnum2: 1,
        totalnum3: 1
      }
    },
    created () {
      this.getData(0)
    },
    activated () {

    },
    methods: {
      handleClick (tab, event) {
        this.getData(0)
      },

      async getData (num) {
        if (this.activeName2 == '1') {
          taskTypeService.search().then(res => {
            this.taskType = res
          })
        } else if (this.activeName2 == '2') {
          plansService.search(num).then(res => {
            this.specialTask = res.data
            this.totalnum1 = parseInt(res.headers['x-total-count'], 10)
          })
        } else if (this.activeName2 == '3') {
          taskObjectService.search().then(res => {
            this.taskObject = res
          })
        } else if (this.activeName2 == '4') {
          dictService.find('ZSLX').then(res => {
            this.certificate = res
            this.totalnum2 = res.length
          })
        } else if (this.activeName2 == '5') {
          let organiza = await orgService.findAll().then(async res => {
            await res.data.sort((a, b) => {
              if(a.businessOrg < b.businessOrg){
                return -1
              }
              if(a.businessOrg > b.businessOrg){
                return 1
              }
              return 0
            })
            this.organiza = res.data
            return res.data
          })
          cruiseService.search().then(res => {
            this.totalnum3 = parseInt(res.headers['x-total-count'], 10)
            let arr = res.data.map(item => {
              organiza.forEach(value => {
                if (value.id == item.organiza.id) {
                  item.name = value.name
                } else {
                  if (value.sub) {
                    value.sub.forEach(element => {
                      if (element.id == item.organiza.id) {
                        item.name = value.name + ' ' +  element.name
                      }
                    })
                  }
                }
              })
              return item
            })
            this.mileage = arr
          })
        } else if (this.activeName2 == '6') {
          punitiveMeasuresService.search().then(res => {
            this.measures = res
          })
        }
      },
    }
  }

</script>

<style scoped lang="scss" src="./Dcustom.scss"></style>
